library(ggplot2)
library(forcats)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(showtext)
## Loading required package: sysfonts
## Loading required package: showtextdb
library(cowplot)
library(magick)
## Linking to ImageMagick 6.9.13.29
## Enabled features: cairo, fontconfig, freetype, heic, lcms, pango, raw, rsvg, webp
## Disabled features: fftw, ghostscript, x11
library(grid)

font_add_google("Nunito Sans", "nunito", regular.wt = 200)
font_add_google("Nunito", "nunito_title", regular.wt = 500)
font_add_google("Nunito", "nunito_subtitle", regular.wt = 200)
font_add_google("Nunito", "nunito_bold", regular.wt = 900)
showtext_auto()

my_data <- read.csv("~/Desktop/Data visualization/KIB_longevity data.csv")
data_prep <- my_data |>
  mutate(Years = as.numeric(Years)) |>
  arrange(Science_strength, Years) |>
  mutate(
    Years_Capped = if_else(Intervention == "Suffer severe mental illness", -12, Years),
    fill_color = case_when(
      Science_strength == 1 ~ "#E57373",
      Science_strength == 2 ~ "#FFB74D",
      Science_strength == 3 ~ "#81C784",
      TRUE ~ "#FFB74D"
    ),
    value_text = paste0(
      ifelse(Years > 0, "+", ""),
      ifelse(Years %% 1 == 0, as.character(Years), as.character(round(Years, 1))),
      " years"
    ),
    label_x_pos = case_when(

      
      Intervention == "Get health checks " ~ 14.5,       
      Intervention == "Have a long-lived maternal grandfather" ~ 27, 
      Intervention == "Live at high altitude" ~ 15.3,
      Intervention == "Have a long-lived sibling" ~ 16,
      Intervention == "Be conscientious " ~ 15,
      Intervention == "Exercise more" ~ 12.5,
      Intervention == "Be optimistic" ~ 15,
      Intervention == "Get promoted" ~ 14.85,
      Intervention == "Have more orgasms" ~ 15.5,
      Intervention == "More Pets" ~ 10,
      Intervention == "With close friends" ~ 15,
      Intervention == "Go to church regularly" ~ 16.2,
      Intervention == "Be polygamous, maybe" ~ 17,
      Intervention == "Live in the country" ~ 14.5,
      Intervention == "Eat less food" ~ 15,
      Intervention == "Hang out with women - a lot!" ~ 20,
      
      Intervention == "Eat red meat" ~ -13,       
      Intervention == "Keep smoking" ~ -11, 
      Intervention == "Sleep too much" ~ -15,
      Intervention == "Live in a city" ~ -15,
      Intervention == "Sit down" ~ -15,
      Intervention == "Suffer severe mental illness" ~ -19,
      
      TRUE ~ if_else(Years_Capped < 0, Years_Capped - 3.5, Years_Capped + 3.5)
    )
  )
create_subplot <- function(subset_data, margin_vec) {
  
  subset_data <- subset_data |>
    mutate(
      Intervention = fct_rev(fct_inorder(Intervention)),
      id = as.integer(Intervention),
      bar_offset = as.numeric(id)
    )

  min_grid <- min(subset_data$bar_offset) - 0.8
  max_grid <- max(subset_data$bar_offset) + 0.8
  
  label_func <- function(x) {
    ifelse(x == 0, "YEARS", as.character(x))
  }

  p <- ggplot(subset_data) +
    geom_segment(
      data = data.frame(x_val = seq(-10, 20, by = 5)),
      aes(x = x_val, xend = x_val, y = min_grid, yend = max_grid),
      color = "#FFDDAA", linewidth = 0.9, alpha = 0.6
    ) +
    geom_col(
      aes(x = Years_Capped, y = bar_offset, fill = fill_color),
      orientation = "y",
      width = 0.75
    ) +
    annotate(
      "segment",
      x = 0, xend = 0, y = min_grid, yend = max_grid,
      color = "gray6", linewidth = 0.4
    ) +
    geom_text(
      aes(
        x = if_else(Years_Capped < 0, -0.2, 0.2),
        y = bar_offset,
        label = Intervention,
        hjust = if_else(Years_Capped < 0, 1, 0)
      ),
      family = "nunito_bold", fontface = "bold", size = 16, color = "black" 
    ) +
    geom_text(
      aes(
        x = label_x_pos,
        y = bar_offset,
        label = value_text,
        hjust = if_else(Years_Capped < 0, 1, 0) 
      ),
      family = "nunito_title", fontface = "italic", size = 12, color = "black" 
    ) +
    scale_fill_identity() +
    scale_y_continuous(NULL, breaks = NULL, labels = NULL, expand = c(0, 1)) +
    coord_cartesian(clip = "off") + 
    scale_x_continuous(
      limits = c(-32, 28), 
      breaks = seq(-10, 20, by = 5),
      labels = label_func,
      position = "top", 
      sec.axis = dup_axis()
    ) +
    theme_void() +
    theme(
      text = element_text(family = "nunito_bold", face = "bold"),
      plot.background = element_rect(fill = "white", color = NA),
      panel.background = element_rect(fill = "white", color = NA),
      axis.text.x = element_text(color = "#444444", size = 50, margin = margin(t = -15, b = 20), face = "bold"), 
      axis.text.x.top = element_text(vjust = 0, margin = margin(b = -15, t = 0)),
      legend.position = "none",
      plot.margin = margin_vec 
    )
  
  return(p)
}
df_suggestive <- data_prep |> filter(Science_strength == 1)
df_good <- data_prep |> filter(Science_strength == 2)
df_strong <- data_prep |> filter(Science_strength >= 3) 

margin_1 <- margin(0, 10, 0, 0)
margin_2 <- margin(0, 20, 0, 20)
margin_3 <- margin(0, 0, 0, -40)

plot_1 <- create_subplot(df_suggestive, margin_1)
plot_2 <- create_subplot(df_good, margin_2)
plot_3 <- create_subplot(df_strong, margin_3)
legend_plot <- ggplot() +
  annotate("rect", xmin = 10, xmax = 13.5, ymin = 38.7, ymax = 39.5, fill = "#E57373", color = NA) +
  annotate("text", x = 11.8, y = 39.15, label = "Suggestive - weak evidences", family = "nunito_bold", fontface = "bold", size = 14.2, color = "black") +
  annotate("rect", xmin = 13.5, xmax = 16.85, ymin = 38.7, ymax = 39.5, fill = "#FFB74D", color = NA) +
  annotate("text", x = 15.17, y = 39.15, label = "Good - moderate evidence", family = "nunito_bold", fontface = "bold", size = 14.2, color = "black") +
  annotate("rect", xmin = 16.85, xmax = 20, ymin = 38.7, ymax = 39.5, fill = "#81C784", color = NA) +
  annotate("text", x = 18.4, y = 39.15, label = "Strong - robust evidence", family = "nunito_bold", fontface = "bold", size = 14.2, color = "black") +
  annotate("text", x = 14.5, y = 40, label = "STRENGTH OF SCIENCE", family = "nunito_bold", fontface = "bold", size = 30, color = "#444444") +
  coord_cartesian(xlim = c(5, 20), ylim = c(39, 43)) +
  theme_void() +
  theme(
    plot.background = element_rect(fill = "white", color = NA),
    text = element_text(family = "nunito_bold", face = "bold")
  )
plots_combined <- plot_grid(
  plot_1, plot_2, plot_3,
  ncol = 3, nrow = 1,
  rel_widths = c(1, 1, 1.05),
  axis = "tb",
  labels = NULL,
  label_size = 0
)
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'nunito_bold' not found in PostScript font database
final_plot <- ggdraw() +
  draw_grob(rectGrob(gp = gpar(fill = "white", col = NA))) +
  
  draw_plot(plots_combined, x = 0, y = 0.05, width = 1, height = 0.85) + 
  
  draw_plot(legend_plot, x = 0.55, y = 0.915, width = 0.45, height = 0.14) + 
  
  draw_label(
    "Live Long - What really extends lifespan?",
    x = 0.05, 
    y = 0.98, 
    hjust = 0, vjust = 1,
    fontfamily = "nunito_title",
    fontface = "bold",
    size = 160, 
    color = "#333333"
  ) +
  
  draw_label(
    "Ranked by Evidence Strength",
    x = 0.05, y = 0.94,
    hjust = 0, vjust = 1,
    fontfamily = "nunito_subtitle",
    fontface = "bold",
    size = 120, 
    color = "#666666"
  )

final_plot

#ggsave("improvement.png", final_plot, width = 30, height = 18)
#knitr::include_graphics("improvement.png")